// pages/shopManager/shopQuota/shopQuota.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        nowDate: '',
        userInfo: null,
        shopType: ['无', '城主', '区级代理', '市级代理', '省级代理'],
        quotaPrice: 0,
        quotaNum: 1,
        isLoading: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            nowDate: new Date().getTime()
        })
        this.getUserInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    getUserInfo: function() {
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let data = {}
        common.post('Storequota/get_store_quota', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    userInfo: res,
                    quotaPrice: res.price
                })
            }
        }).catch(function(e) {})
    },

    goMinus: function() {
        let num = this.data.quotaNum
        if (num < 2) {
            return
        }
        this.setData({
            quotaNum: num - 1
        })
    },

    goAdd: function() {
        let num = this.data.quotaNum
        this.setData({
            quotaNum: num + 1
        })
    },

    goBuy: function() {
        if (this.data.isLoading) {
            return
        }
        wx.showLoading({
            title: '请稍后'
        })
        this.setData({
            isLoading: true
        })
        const that = this
        wx.login({
            success(res) {
                if (res.code) {
                    //发起网络请求
                    let data = {
                        code: res.code,
                        apply_type: 2,
                        num: that.data.quotaNum,
                        pay_client: 1
                    }
                    common.post('Storequota/apply_up_store', data).then(function(res) {
                        that.setData({
                            isLoading: false
                        })
                        wx.hideLoading()
                        if (res.code == 204) {
                            wx.showModal({
                                title: '提示',
                                content: res.error,
                                showCancel: false,
                                success(res) {
                                    if (res.confirm) {

                                    }
                                }
                            })
                            return
                        }
                        if (res.type == 1) {
                            wx.showModal({
                                title: '提示',
                                content: '申请成功，请联系邀请人进行审核',
                                showCancel: false,
                                success(res) {
                                    if (res.confirm) {
                                        that.setData({
                                            quotaPrice: 0,
                                            quotaNum: 1
                                        })
                                        common.refresh()
                                    }
                                }
                            })
                        } else {
                            wx.requestPayment({
                                timeStamp: res.timeStamp,
                                nonceStr: res.nonceStr,
                                package: res.package,
                                signType: res.signType,
                                paySign: res.paySign,
                                success(res) {
                                    wx.showModal({
                                        title: '提示',
                                        content: '支付成功',
                                        showCancel: false,
                                        success(res) {
                                            if (res.confirm) {
                                                that.setData({
                                                    quotaPrice: 0,
                                                    quotaNum: 1,
                                                    isLoading: false
                                                })
                                                common.refresh()
                                            }
                                        }
                                    })
                                },
                                fail(res) {
                                    wx.showToast({
                                        title: '支付失败',
                                        icon: 'none',
                                        duration: 1000
                                    })
                                }
                            })
                        }
                    }).catch(function(e) {})
                }
            }
        })
    }
})